/*
 * relative     : \src\unocss\rules.ts
 * Author       : 郭鹏超
 * Date         : 2024-08-24 15:02
 * LastEditors  : SJZ
 * LastEditTime : 2025-03-27 23:19
 * Description  :规则
 * CodeIterationRecord:
 */

const rules = [
  //font-size
  [/^fs-(\d+)$/, ([, d]) => ({ "font-size": `${d}px` })],
  //font-weight
  [/^fw-(\d+)$/, ([, d]) => ({ "font-weight": `${d}` })],
  //height
  [/^h-(\d+)$/, ([, d]) => ({ height: `${d}px` })],
  [/^hl-(\d+)$/, ([, d]) => ({ height: `${d}px`, "line-height": `${d}px` })],
  //宽
  [
    /^w-(\d+|datetime|date|time)$/,
    ([, value]) => {
      let width: string;
      switch (value) {
        // 时间日期宽度
        case "datetime":
          width = "130px";
          break;
        // 日期宽度
        case "date":
          width = "100px";
          break;
        // 时间宽度
        case "time":
          width = "80px";
          break;
        default:
          width = `${value}px`;
      }
      return { width };
    }
  ],
  // 边框border
  [/^b-(\d+)$/, (match) => ({ "border-width": `${match[1]}px` })],
  [/^b-(\d+)-#([\w]+)$/, (match) => ({ border: `solid ${match[1]}px #${match[2]}` })],
  [/^bt-(\d+)-#([\w]+)$/, (match) => ({ "border-top": `solid ${match[1]}px #${match[2]}` })],
  [/^bb-(\d+)-#([\w]+)$/, (match) => ({ "border-bottom": `solid ${match[1]}px #${match[2]}` })],
  [/^bl-(\d+)-#([\w]+)$/, (match) => ({ "border-left": `solid ${match[1]}px #${match[2]}` })],
  [/^br-(\d+)-#([\w]+)$/, (match) => ({ "border-right": `solid ${match[1]}px #${match[2]}` })],
  //padding
  [/^p-(\d+)$/, (match) => ({ padding: `${match[1]}px` })],
  [/^px-(\d+)$/, (match) => ({ "padding-left": `${match[1]}px`, "padding-right": `${match[1]}px` })],
  [/^py-(\d+)$/, (match) => ({ "padding-top": `${match[1]}px`, "padding-bottom": `${match[1]}px` })],
  [/^pt-(\d+)$/, (match) => ({ "padding-top": `${match[1]}px` })],
  [/^pb-(\d+)$/, (match) => ({ "padding-bottom": `${match[1]}px` })],
  [/^pl-(\d+)$/, (match) => ({ "padding-left": `${match[1]}px` })],
  [/^pr-(\d+)$/, (match) => ({ "padding-right": `${match[1]}px` })],
  //margin
  [/^m-(\d+)$/, (match) => ({ margin: `${match[1]}px` })],
  [/^mx-(\d+)$/, (match) => ({ "margin-left": `${match[1]}px`, "margin-right": `${match[1]}px` })],
  [/^my-(\d+)$/, (match) => ({ "margin-top": `${match[1]}px`, "margin-bottom": `${match[1]}px` })],
  [/^mt-(\d+)$/, (match) => ({ "margin-top": `${match[1]}px` })],
  [/^mb-(\d+)$/, (match) => ({ "margin-bottom": `${match[1]}px` })],
  [/^ml-(\d+)$/, (match) => ({ "margin-left": `${match[1]}px` })],
  [/^mr-(\d+)$/, (match) => ({ "margin-right": `${match[1]}px` })],
  ["flex-1", { flex: 1 }],
  //背景图片铺满父级元素
  [/^bgi-full-(.*)$/, (match) => ({ background: `url(${match[1]})`, "background-size": "100% 100%" })],
  [/^bgs-(.*)-(.*)$/, (match) => ({ "background-size": `${match[1]} ${match[2]}` })],
  //px计算
  [/^hc-(\d+)$/, ([, d]) => ({ height: `calc(100% - ${d}px) !important` })],
  [/^wc-(\d+)$/, ([, d]) => ({ width: `calc(100% - ${d}px) !important` })],
  ["text-align-last-justify", { "text-align-last": "justify" }],
  /**
   * @description: 设置背景颜色
   */
  [
    /^bgc-(.*)$/,
    (match) => {
      const color = `${match[1]}`;
      return { "background-color": `${color}` };
    }
  ],
  /**
   * @description: 设置主题 背景颜色 字体颜色 边框颜色
   */
  [
    /^theme-(bgc|color|bc)-(\w+)$/,
    (match, { theme }) => {
      const mapping = {
        bgc: "background-color",
        color: "color",
        bc: "border-color"
      };
      const property = mapping[match[1]];
      const colorName = match[2];
      const colorValue = theme[colorName];
      if (colorValue) {
        return { [property]: `${colorValue} !important` }; // 返回样式，添加 !important
      }
      return {};
    }
  ],
  /**
   * @description: 设置按钮颜色
   */
  [
    /^button-(\w+)$/,
    (match, { theme }) => {
      const colorName = match[1];
      const colorValue = theme[colorName];
      if (colorValue) {
        return { "background-color": `${colorValue} !important`, color: colorName == "default" ? `${theme["fontColor"]} !important` : "" }; // 返回样式，添加 !important
      }
      return {};
    }
  ],
  //渐变
  [
    /^lgt-(.*?)-(.*?)$/, // 修改正则表达式以匹配两个颜色参数
    (match, { theme }) => {
      const colorValue1 = match[1]; // 第一个颜色参数
      const colorValue2 = match[2]; // 第二个颜色参数

      // 从主题中获取颜色值，若不存在则使用原始颜色名
      const resolvedColor1 = theme[colorValue1] ? theme[colorValue1] : colorValue1;
      const resolvedColor2 = theme[colorValue2] ? theme[colorValue2] : colorValue2;
      // 返回线性渐变样式
      return {
        background: `linear-gradient(to right, ${resolvedColor1}, ${resolvedColor2})`
      };
    }
  ],
  //阴影
  [
    /^system-shadow-(.*?)-(.*?)-(.*?)-(.*?)$/, // 修改正则表达式以匹配两个颜色参数
    (match) => {
      const css = `${match[1] ?? "2"}px ${match[2] ?? "2px"}px ${match[3] ?? "1"}px ${match[4] ?? "0"}px ${
        match[5] ?? "rgba(0, 0, 0, 0.2)"
      }`;
      return {
        "box-shadow": `${match[1] ?? "2"}px ${match[2] ?? "2px"}px ${match[3] ?? "1"}px ${match[4] ?? "0"}px rgba(0, 0, 0, 0.2)`
      };
    }
  ]
];
export default rules;
